// const bodyParser = require('body-parser');
let express = require('express')
let qs = require('querystring')
let login = express.Router();
let {pool} = require('../db/pool')
var session = require('express-session');
const { connect } = require('http2');
const jwt = require('jsonwebtoken');
login.get('/login',(req,res)=>{

    let username = req.query.username;
    let password = req.query.password;
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        connection.query(`select * from base_user where user_name='${username}'`,(err,result)=>{
            if(err) throw err;
            // console.log(result)
            if(result.length){
                if(password == result[0].user_password){
                    
                    let payload = {name:username,admin:true};
                    let secret = 'I_LOVE_MYSELF';
                    let token = jwt.sign(payload,secret,{expiresIn:60*60*3});
                    res.send({status:200,message:"OK",token:token})
                }else{
                    res.send({status:400,message:"密码错误"})
                }
            }else{
                res.send({status:500,message:'该用户不存在'})
            }
            connection.release() 
        })
    })
})
module.exports = {login}